/*
  学习目标：setState的第二个参数
  特点:
    1. 不会合并setState, 不会减少render执行册数, 造成性能浪费
    2. 容易造成回调地域
  👎 早期写法
 
*/
import React, { Component } from 'react';

export default class App extends Component {
  state = {
    count: 0,
  };

  handleAdd = () => {
    this.setState({ count: this.state.count + 1 }, () => {
      this.setState({ count: this.state.count + 2 }, () => {
        this.setState({ count: this.state.count + 3 });
      });
    });

    // ❌ 下面会造成count更新为3, 因为闭包
    // const { count } = this.state
    // this.setState({ count: count + 1 }, () => {
    //   this.setState({ count: count + 2 }, () => {
    //     this.setState({ count: count + 3 });
    //   });
    // });
  };
  render() {
    console.log(' render ----->  ');
    const { count } = this.state;
    return (
      <div>
        <h1>{count}</h1>
        <button onClick={this.handleAdd}>点我</button>
      </div>
    );
  }
}
